﻿namespace Sidvall.UI.Wpf.Views
{
	public class ContentControlRegionManager : Sidvall.UI.Views.IRegionManager
	{
		#region Public Members

		public object GetRegionContainer(object container, string regionName)
		{
			System.Windows.Controls.ContentControl regionControl;

			regionControl = GetRegionControl(container, regionName);
			if (regionControl == null)
				return null;
			return regionControl.Content;
		}
		public object[] GetViews(object container, string regionName)
		{
			System.Windows.Controls.ContentControl regionControl;

			regionControl = GetRegionControl(container, regionName);
			if (regionControl == null)
				return null;
			if (regionControl.Content == null)
				return null;
			return new object[] { regionControl.Content };
		}
		public bool RemoveView(object container, string regionName)
		{
			System.Windows.Controls.ContentControl regionControl;

			regionControl = GetRegionControl(container, regionName);
			if (regionControl == null)
				return false;
			regionControl.Content = null;

			return true;
		}
		public bool ShowView(object container, string regionName, object view)
		{
			System.Windows.Controls.ContentControl regionControl;

			regionControl = GetRegionControl(container, regionName);
			if (regionControl == null)
				return false;
			regionControl.Content = view;
			return true;
		}
		public bool CanShowView(object container, string regionName)
		{
			System.Windows.Controls.ContentControl regionControl;

			regionControl = GetRegionControl(container, regionName);
			if (regionControl == null)
				return false;
			return true;
		}

		#endregion
		#region Private Members

		private static System.Windows.Controls.ContentControl GetRegionControl(object container, string regionName)
		{
			System.Windows.FrameworkElement element;

			element = container as System.Windows.FrameworkElement;
			if (element == null)
				return null;
			return element.FindName(regionName) as System.Windows.Controls.ContentControl;
		}

		#endregion
	}
}
